﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;
using IrishSettings.Commands;

namespace IrishSettings.TableControllers
{
    public class ApplicationAndVersionController : TableController<SettingsApplication>
    {
        public ApplicationAndVersionController(DataFactory factory, DbConnection connection, DbTransaction transaction)
            : base(factory,connection, transaction)
        {
            if(connection==null)
                throw new ArgumentException("connection parameter required, can't be null");
            tableName = "{$ns}_settings_applications";

            sharedConnection = connection;
            columns = new Dictionary<string,DbType>
                          { 
                              {"id",DbType.Int32},
                              {"application",DbType.String},
                              {"version",DbType.String},
                              {"is_active_version",DbType.String},
                              {"culture",DbType.String},
                              {"current_version",DbType.String},
                              {"machine_name",DbType.String},
                              {"domain_name",DbType.String},
                              {"app_settings_key",DbType.String},
                          };
        }

        public override SettingsApplication Select(long id)
        {
            return ProcessSelect(id);
        }

        public override List<SettingsApplication> SelectList(int from, int to)
        {
            return ProcessSelectList(from, to);
        }

        public override SettingsApplication Insert(SettingsApplication setting)
        {
            return ProcessInsert(setting);
        }

        public override SettingsApplication Update(SettingsApplication setting)
        {
            return ProcessUpdate(setting);   
        }

        public override void Delete(long id)
        {
            ProcessDelete(id);
        }

        public override bool Exists(long id)
        {
            return ProcessExists(id);
        }

        public SettingsApplication SelectByApplicationName(string application, Version version)
        {
            using (SelectApplicationByName command = new SelectApplicationByName(factory, sharedConnection, sharedTransaction))
            {
                long id;
                using (DbDataReader reader = command.SelectOne(application, version))
                {
                    if(reader.HasRows)
                    {
                        reader.Read();
                        id = Convert.ToInt64(reader.GetValue(0));
                    }
                    else
                    {
                        throw new InvalidOperationException("There isn't an application/version of (" +application + " + " + version + ") in the database");
                    }
                    
                }
                return ProcessSelect(id);
            }
        }

        public bool ExistsByApplicationName(string application, Version version)
        {
            using (SelectApplicationByName command = new SelectApplicationByName(factory, sharedConnection, sharedTransaction))
            {
                using(DbDataReader reader= command.SelectOne(application, version))
                {
                    if (reader.HasRows)
                        return true;
                    else
                    {
                        return false;
                    }
                }
            }
        }
    }
}
